package com.game.league.task;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.game.base.app.App;
import com.game.base.app.base.BaseFragment;
import com.game.base.app.callback.ItemOnClick;
import com.game.base.glideV4.ImageLoader;
import com.game.base.utils.DecimalUtil;
import com.game.base.utils.log.Logger;
import com.game.base.utils.time.FormatPattern;
import com.game.base.utils.time.TimeUtil;
import com.game.base.utils.toast.ToastUtil;
import com.game.league.R;
import com.game.league.databinding.FragmentTaskBinding;
import com.game.league.entrance.user.UserMgr;
import com.game.league.entrance.user.table.User;
import com.game.league.service.InterfaceMgr;
import com.game.league.task.adapter.TaskAdapter;
import com.game.league.task.table.Task;
import com.game.league.util.Constants;
import com.game.league.util.SkipUtil;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 任务
 * Created by Su on 2019/5/27.
 */
public class TaskFragment extends BaseFragment<FragmentTaskBinding> implements View.OnClickListener, ItemOnClick {
    private User user;
    private TaskAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task;
    }

    @Override
    protected void initView() {
        binding.setTitle("更多");
        binding.setClick(this);

        refreshView();
        ImageLoader.loadCircleAvatar(mActivity, user.getAvatar(), binding.ivHead);
        binding.tvNick.setText(user.getNickname());

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        binding.recyclerList.setLayoutManager(layoutManager);
        adapter = new TaskAdapter(mActivity, this);
        binding.recyclerList.setAdapter(adapter);
    }

    public void refreshView() {
        user = UserMgr.getCurrentUser();
        if (binding == null || binding.btnSign == null) return;
        binding.btnSign.setText(isSign() ? "已签到" : "签到");
        binding.tvCoinToday.setText("今日已获得: " + DecimalUtil.getFormatFloat(user.getCoinToday()));
    }

    @Override
    protected void initData() {
        InterfaceMgr.getInstance().getTaskList(Constants.TASK_LIST, new FindListener<Task>() {
            @Override
            public void done(List<Task> list, BmobException e) {
                if (list == null || list.isEmpty()) return;
                adapter.setItems((ArrayList<Task>) list);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sign:
                handleSign();
                break;
        }
    }

    private void handleSign() {
        if (isSign()) {
            return;
        }
        user.setSignAt(TimeUtil.formatTime(App.time, FormatPattern.FP_YMD));
        user.addCoinNum(user, InterfaceMgr.getConfig().getSignReward());
        UserMgr.updateUser(user, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    SkipUtil.showSignDialog(mActivity);
                } else {
                    Logger.printThrowable(e);
                }
            }
        });
    }

    private boolean isSign() {
        if (App.time == null) {
            App.time = System.currentTimeMillis();
        }
        String today = TimeUtil.formatTime(App.time, FormatPattern.FP_YMD);
        return TextUtils.equals(today, user.getSignAt());
    }

    @Override
    public void onItemClick(int viewId, int position, Object o) {
        if (position == adapter.getItemCount() - 1) {
            ToastUtil.showLong("更多任务加紧筹备中...");
            return;
        }
        SkipUtil.showTaskListActivity(mActivity);
    }
}
